﻿using STHPMS1.BLL.B_Core;
using STHPMS1.ICore.I_BLL.Bus;
using STHPMS1.ICore.I_Entity.Info;
using STHPMS1.ICore.I_Entity.Stmpms1;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using STHPMS1.Common.C_Entity.Stmpms1;
using System.Linq;
using System.Collections;
using STHPMS1.ICore.I_DAL;
using STHPMS1.ICore.I_Serv;
using STHPMS1.ICore;

namespace STHPMS1.BLL.B_Bus
{
    public class Dp_Preven_DetailBus : BllBase, IDp_Preven_DetailBill
    {
        public void Save<T>(T entity)
            where T : class ,IDp_Preven_Detail
        {
            if (this.Scene.Dal.Sthpms1.Update(entity) == 0)
            {
                this.Scene.Dal.Sthpms1.Insert(entity);
            }
        }

        public void Remove(IDp_Preven_Detail entity)
        {
            this.Scene.Dal.Sthpms1.Delete(entity);
        }

        public IEnumerable<T> Get<T>(Expression<Func<T, bool>> predicate, IPagingInfo info)
            where T : class, IDp_Preven_Detail
        {
            return this.Scene.Dal.Sthpms1.Select<T>(predicate, info,k=>k.Mainsegid,false);
        }

        /// <summary>
        /// 获取分析年度的所有养护路段
        /// </summary>
        /// <param name="rdwayid"></param>
        /// <returns></returns>
        public List<string> GetMainSegid(string anniname, string analy_year)
        {
            return this.Scene.Dal.Sthpms1.Dp_Preven_Detail.GetMainSegid(anniname, analy_year);
        }
        /// <summary>
        /// 更新等效年度费用
        /// </summary>
        /// <param name="rdwayid"></param>
        public void SetEAC(string anniname, string analy_year)
        {
            ISthpms1Dao dao = this.Scene.Dal.Sthpms1;

            //this.Scene.Dal.Sthpms1.Dp_Preven_Detail.SetEAC(analy_year); //更新等效年度费用

            List<Dp_Preven_Detail> data = this.Scene.Bll.Dp_Preven_Detail.Get<Dp_Preven_Detail>(P => P.Anni_Name == anniname && P.Analy_Year == analy_year, null).ToList<Dp_Preven_Detail>();
            foreach(Dp_Preven_Detail info in data)
            {
                IB_Invntory ib_invntory = this.Scene.Bll.Dp_Dailymain_Cost.GetB_Invntory(info.Mainsegid); //基础路段信息

                Dn_Divi_Result dndivinfo = dao.Single<Dn_Divi_Result>(p => p.Mainsegid == info.Mainsegid);
                //double area = this.Scene.Bll.Dn_Divi_Result.GetRoadAera(info.Rdwayid, info.Mainsegid);  //计算面积
                double area = Convert.ToDouble((dndivinfo.Endmp-dndivinfo.Beginmp)*ib_invntory.Surfwidth);  //计算面积

                //dao.Update<Dp_Preven_Detail>(new { Cost = (area * info.Unit_Cost)/10000 }, p => p.Rdwayid == info.Rdwayid && p.Mainsegid == info.Mainsegid);

                dao.Update<Dp_Preven_Detail>(new { Eac = info.Unit_Cost / info.Servi_Life, Cost = (area * info.Unit_Cost) / 10000 }, p => p.Id == info.Id);  //计算总费用
            }
        }
        /// <summary>
        /// 更新复选措施 设置为1
        /// </summary>
        /// <param name="rdwayid"></param>
        /// <param name="values"></param>
        public void SetSECON_MEASURE(string anniname, string analy_year, int values)
        {
            List<string> list = GetMainSegid(anniname, analy_year);
            foreach (string mainsegid in list)
            {
                this.Scene.Dal.Sthpms1.Dp_Preven_Detail.SetSECON_MEASURE(anniname, analy_year, mainsegid, values);
            }
        }
       

        

       
        /// <summary>
        /// 获取当前路线的所有初选措施   
        /// </summary>
        /// <param name="rdwayid"></param>
        /// <returns></returns>
        public List<string> GetMeas_ini(string rdwayid)
        {
            return this.Scene.Dal.Sthpms1.Dp_Preven_Detail.GetMeas_ini(rdwayid);
        }

       /// <summary>
        /// 获取养护年份 复选措施和终选措施都为1
       /// </summary>
       /// <param name="mainsegid"></param>
       /// <returns></returns>
        public string GetMainYear(string mainsegid)
        {
            return this.Scene.Dal.Sthpms1.Dp_Preven_Detail.GetMainYear(mainsegid);
        }

        /// <summary>
        /// 获取总费用 复选措施和终选措施都为1
        /// </summary>
        /// <param name="mainsegid"></param>
        /// <returns></returns>
        public double GetCost(string mainsegid)
        {
            return this.Scene.Dal.Sthpms1.Dp_Preven_Detail.GetCost(mainsegid);
        }


       

    }
}
